<?php
namespace app\index\controller;

use app\index\model\Img;
use FileManager\FileManager as Resource;
use think\Config;
use think\Controller;

class FileManager extends Controller
{
    protected static $imgExt = array("gif", "jpeg", "jpg", "png", "bmp");

    public function _initialize()
    {
        $this->IMG_TAG_FOLDER    = Config::get('static_config') . "\img\\tag";
        $this->IMG_NOTICE_FOLDER = Config::get('static_config') . "\img\\notice";
    }

    /**
     * 获取指定路径目录中的文件信息
     */
    public function getResource($path)
    {
        try {

            $fm  = new Resource($path);
            $rtn = [];

            foreach ($fm->scan() as $doc) {
                $row         = [];
                $row['path'] = dirname($doc->path);
                $row['ext']  = $doc->extName;

                //根据图像类型获取图像宽高

                if ($this->isImage(strtolower($doc->extName))) {
                    $img_info = getimagesize($doc->path);

                    $row['width']  = $img_info[0];
                    $row['height'] = $img_info[1];
                }
                $row['filename']    = basename($doc->path);
                $row['filesize']    = $doc->filesize;
                $row['lastmodtime'] = date('Y-m-d H:i:s', $doc->lastModTime);

                //排除文件目录

                if (!empty($row['ext'])) {
                    array_push($rtn, $row);
                }

            }
            return ($rtn);

        } catch (\Exception $e) {
            throw new \think\Exception('异常消息:' . $e->getMessage());
        }
    }

    /**
     * 图像文件扩展类型判断
     */
    public function isImage($fileExt)
    {
        return in_array($fileExt, self::$imgExt) ? true : false;
    }
    /**
     * [doList description]
     * 图像文件信息
     * @DateTime 2017-11-22
     * @param    string $path 文件目录
     * @return   array 信息数组
     */
    public function doList($path)
    {

        try {

            $rows = $this->getResource($path);

            //查询数据总计
            $total = count($rows);
            $res   = array();

            if ($total > 0) {

                $res['rows']  = $rows;
                $res['total'] = $total;
            } else {

                $res['total'] = 0;
                $res['rows']  = array();
            }

            _callback($res);

        } catch (\Exception $e) {
            throw new \think\Exception($e->getMessage());
        }
    }

    /**
     * 公告图像目录文件
     */

    public function noticeList()
    {
        $this->doList($this->IMG_NOTICE_FOLDER);
    }

    /**
     * 单个图像删除
     */
    public function deleteImg()
    {
        $this->delete(new Img());
    }

    /**
     * 批量删除图像
     */
    public function batDeleteImg()
    {
        $this->batDelete(new Img());
    }

    /**
     * 文件删除
     */
    public function delete($model)
    {

        try {

            if (file_exists(input('url'))) {

                // 实体文件删除

                $fm  = new Resource(input('url'));
                $res = $fm->del();

                // 数据库记录删除

                $model->destroy(function ($query) {
                    $query->where('filename', '=', basename(input('url')))
                        ->where('path', '=', dirname(input('url')));
                });

                $output['success'] = $res ? true : false;
                $output['msg']     = $res ? "文件删除成功" : "文件删除失败";

                _callback($output);
            } else {

                $output['success'] = false;
                $output['msg']     = "文件不存在";

                _callback($output);
            }

        } catch (\Exception $e) {
            throw new \think\Exception('异常消息:' . $e->getMessage());
        }
    }
    /**
     * 批量删除
     */
    public function batDelete($model)
    {

        try {

            //根据对象路径,删除数据

            $arr = json_decode(input("get.path"), true);

            foreach ($arr as $path) {

                if (file_exists($path)) {

                    //文件删除

                    $fm  = new Resource($path);
                    $res = $fm->del();

                    //数据库记录删除

                    $file = $model->where('filename', basename($path))
                        ->where('path', dirname($path))
                        ->find();

                    if ($file) {

                        $file->delete();

                    } else {

                        $output['success'] = false;
                        $output['msg']     = "文件记录不存在";

                        _callback($output);
                    }

                } else {
                    $output['data'] = $path;

                    $output['success'] = false;
                    $output['msg']     = "文件不存在";

                    _callback($output);
                }

            }

            $output['msg']     = '删除成功';
            $output['success'] = true;

            _callback($output);

        } catch (\Exception $e) {
            throw new \think\Exception('异常消息:' . $e->getMessage());
        }
    }
}
